#Boa:Frame:FAsistencia

import wx
import os
import time
import psycopg2
import psycopg2.extras
import sys
import pprint
from threading import Timer

def create(parent):
    return FAsistencia(parent)
Codigo_catedratico=0
codigo=''
[wxID_FASISTENCIA, wxID_FASISTENCIABENTRADA, wxID_FASISTENCIABMENU, 
 wxID_FASISTENCIABSALIDA, 
] = [wx.NewId() for _init_ctrls in range(4)]

class FAsistencia(wx.Frame):
    Codigo_catedratico=0
    codigo=''
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FASISTENCIA, name=u'FAsistencia',
              parent=prnt, pos=wx.Point(506, 167), size=wx.Size(300, 220),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Asistencia')
        self.SetClientSize(wx.Size(300, 220))
        self.Inserta_Foto("imagen/menu_background.png",(0,0))

        ## Entrada
        bmpEntrada = wx.Image("imagen/boton_entrada.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.bentrada = wx.BitmapButton(self,-1, bmpEntrada,pos=wx.Point(20,20),style=wx.NO_BORDER)
        self.Bind(wx.EVT_BUTTON, self.OnBentradaButton,self.bentrada)

        ## Salida
        bmpSalida = wx.Image("imagen/boton_salida.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.bsalida = wx.BitmapButton(self,-1, bmpSalida,pos=wx.Point(20,80),style=wx.NO_BORDER)
        self.Bind(wx.EVT_BUTTON, self.OnBsalidaButton,self.bsalida)

         ## Atras
        bmpAtras = wx.Image("imagen/boton_atras.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.bmenu = wx.BitmapButton(self,-1, bmpAtras,pos=wx.Point(210,20),style=wx.NO_BORDER)
        self.Bind(wx.EVT_BUTTON, self.OnBmenuButton, self.bmenu)

    def __init__(self, parent,cod):
        global codigo
        global Codigo_catedratico
        codigo=cod
        Codigo_catedratico=codigo
        print codigo
        print Codigo_catedratico
        self._init_ctrls(parent)

    def OnBentradaButton(self, event):
        #dlg = wx.MessageDialog(self, 'Ingrese Huella por 10 Seg', 'Info' )
        #wx.FutureCall(10000,dlg.Destroy)
        #dlg.ShowModal()
        #dlg1 = wx.MessageDialog(self, 'Ingrese Huella por 10 Seg', 'Info' )
        #wx.FutureCall(10000,dlg1.Destroy)
        #dlg1.ShowModal()
        #dlg.Destroy()
        #mins = 0
        # Only run if the user types in "start"
        #if run == "start":
        # Loop until we reach 20 minutes running
##        while mins != 10:
##            print ">>>>>>>>>>>>>>>>>>>>>", mins
##            # Sleep for a minute
##            time.sleep(1)
##            # Increment the minute total
##            mins += 1
        #dlg = wx.MessageDialog(self, 'Datos Entrada Guardados', 'Info', wx.OK | wx.ICON_INFORMATION)
        
        #Codigo_catedratico = codigo
        print Codigo_catedratico
        
        if Codigo_catedratico!=0:
            Entrada = 'CURRENT_TIME'
            Salida = 'CURRENT_TIME'
            Fecha = 'CURRENT_DATE'
            Observacion = 'guardado'
            conn_string = "host='localhost' port='5432' dbname='DB778' user='postgres' password='postgres'" 
            conn = psycopg2.connect(conn_string)
            cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
            cursor.execute("INSERT INTO horario(entrada, codigo_catedratico, salida, fecha, observacion) VALUES (%s, %s, %s, %s, '%s')" % (Entrada,Codigo_catedratico,Salida,Fecha,Observacion))
            conn.commit()
            cursor.close()
            conn.close()
            
		    
        dlg1 = wx.MessageDialog(self, 'Registrado', 'Info', wx.OK | wx.ICON_INFORMATION)
	wx.FutureCall(3000,dlg1.Destroy)
        dlg1.ShowModal()
        #dlg.Destroy()
        self.Close(True)
        event.Skip()

    def OnBsalidaButton(self, event):
        #dlg = wx.MessageDialog(self, 'Ingrese Huella por 10 Seg', 'Info', wx.OK | wx.ICON_INFORMATION)
        #dlg.ShowModal()
        #dlg.Destroy()
        
        #mins = 0
        # Only run if the user types in "start"
        #if run == "start":
        # Loop until we reach 20 minutes running
        #while mins != 10:
        #    print ">>>>>>>>>>>>>>>>>>>>>", mins
        #    # Sleep for a minute
        #    time.sleep(1)
        #    # Increment the minute total
        #    mins += 1
        #dlg = wx.MessageDialog(self, 'Datos Salida Guardados', 'Info', wx.OK | wx.ICON_INFORMATION)
        
        #Codigo_catedratico = codigo
        print Codigo_catedratico
        
        if Codigo_catedratico!=0:
            
            Salida = 'CURRENT_TIME'
	    Fecha = 'CURRENT_DATE'
            Observacion = 'guardado'
            conn_string = "host='localhost' port='5432' dbname='DB778' user='postgres' password='postgres'" 
            conn = psycopg2.connect(conn_string)
            cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
            cursor.execute("UPDATE horario SET salida=%s, observacion='%s' WHERE codigo_catedratico=%s AND fecha=%s" % (Salida,Observacion,Codigo_catedratico,Fecha))
            conn.commit()
            cursor.close()
            conn.close()
            
		    
        dlg2 = wx.MessageDialog(self, 'Registrado', 'Info', wx.OK | wx.ICON_INFORMATION)
        wx.FutureCall(3000,dlg2.Destroy)
        dlg2.ShowModal()
        self.Close(True)
        event.Skip()

    def OnBmenuButton(self, event):
        self.Close(True)
        event.Skip()
        
    def Inserta_Foto(self, imagenName, pos):
        imagen = open(imagenName, 'rb')
        Img = wx.BitmapFromImage(wx.ImageFromStream(imagen))
        wx.StaticBitmap(self, -1, Img, pos)
